package com.dongdongshop.service.impl;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.dongdongshop.data.PageResult;
import com.dongdongshop.domain.rep.SpecificationOptionReq;
import com.dongdongshop.domain.rep.SpecificationReq;
import com.dongdongshop.domain.res.SpecificationOptionRes;
import com.dongdongshop.domain.res.SpecificationRes;
import com.dongdongshop.mapper.SpecMapper;
import com.dongdongshop.mapper.SpecOptionMapper;
import com.dongdongshop.model.Specification;
import com.dongdongshop.model.SpecificationOption;
import com.dongdongshop.service.SpecService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private SpecMapper specMapper;

    @Autowired
    private SpecOptionMapper specOptionMapper;


    /**
     * 规格分页查询
     * @param specificationReq
     * @return
     */
    @Override
    public PageResult queryPage(SpecificationReq specificationReq) {
        Specification specification = new Specification();
        BeanUtils.copyProperties(specificationReq, specification);
        PageHelper.startPage(specificationReq.getPageNum(), specificationReq.getPageSize());
        Page<Specification> page=specMapper.queryPage(specification);
        return new PageResult<>(page.getPageNum(), page.getPages(), page.getTotal(), page.getResult());
    }


    /**
     * 通过specId查询Option所有数据
     * @param id
     * @return
     */
    @Override
    public List<SpecificationOptionRes> queryOptionBySpecId(Long id) {
       List<SpecificationOption> optionList= specOptionMapper.queryOptionBySpecId(id);
        List<SpecificationOptionRes> optionRes = optionList.stream().map(option -> {
            SpecificationOptionRes specOptionRes = new SpecificationOptionRes();
            BeanUtils.copyProperties(option, specOptionRes);
            return specOptionRes;
        }).collect(Collectors.toList());
        return optionRes;
    }


    /**
     * 增加修改操作
     * @param specificationReq
     * @return
     */
    @Override
    @Transactional
    public void save(SpecificationReq specificationReq) {

        Specification specification = new Specification();
        BeanUtils.copyProperties(specificationReq, specification);

        if (specificationReq.getId() == null) {
            specMapper.add(specification);
            Long id = specification.getId();
            List<SpecificationOption> specificationOptionList = specificationReq.getSpecOption().stream().map(option -> {
                SpecificationOption specOption = new SpecificationOption();
                BeanUtils.copyProperties(option, specOption);
                specOption.setSpecId(id);
                return specOption;
            }).collect(Collectors.toList());
            specOptionMapper.add(specificationOptionList);
        }
        specMapper.update(specification);
        Long id = specification.getId();
        List<SpecificationOption> specificationOptionList = specificationReq.getSpecOption().stream().map(option -> {
            SpecificationOption specOption = new SpecificationOption();
            BeanUtils.copyProperties(option, specOption);
            specOption.setSpecId(id);
            return specOption;
        }).collect(Collectors.toList());
        specOptionMapper.deleteBySpecId(id);
        specOptionMapper.add(specificationOptionList);
    }


    /**
     * 规格删除操作
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        specMapper.delete(id);
        specOptionMapper.deleteBySpecId(id);
    }


    /**
     * 规格批量删除操作
     * @param ids
     */
    @Transactional
    @Override
    public void batchRemove(List<Integer> ids) {
        specMapper.batchRemove(ids);
        specOptionMapper.batchRemoveBySpecId(ids);
    }
}
